<?php
namespace app\api\controller;
use app\api\controller\Base;
use app\service\LoginService;
use app\service\UserService;
use app\common\model\User;
use app\common\model\Mall;

use EasyWeChat\Factory;

class Login extends AllBase
{
    public $openPlatform;
    public function __construct () {
        parent::__construct();
    }

    //微信登陆
    public function wechat(){
        $input = input();
        if(!$input['url'] || !$input['mall_id']) {
            _pr('error');
        }
        if($input['url']) {
            $url = $input['url'];
            $url = str_replace('%26','&',$url);
            cookie('redirect_url',$url);
        }
        //$officialAccount = $this->openPlatform->officialAccount($this->mall['authorizer_appid'],$this->mall['authorizer_refresh_token']);
        $officialAccount = $this->officialAccount;
        $oauth = $officialAccount->oauth->scopes(['snsapi_userinfo']);
        return $oauth->redirect()->send();
    }

  

    //微信回调
    public function wechat_callback(){
        cookie('user_id',null);
        $officialAccount = $this->officialAccount;
        //$officialAccount = $this->openPlatform->officialAccount($this->mall['authorizer_appid'],$this->mall['authorizer_refresh_token']);
        $user = $officialAccount->oauth->user()->toArray();
        $wechatUser = $user['original'];
        $wechatUser['mall_id'] = $this->mall['id'];

        $apiUserInfo = $officialAccount->user->get($wechatUser['openid']);//接口获取的用户信息包含是否关注
        $wechatUser['subscribe'] = $apiUserInfo['subscribe'];

        $user_id = UserService::S()->handleWechatUser($wechatUser);
        cookie('user_id',$user_id);
        $userInfo = User::getOneById($user_id);
        /* if(!$userInfo['openid_main']) {
            $config = [
                'app_id'   => config('open_appid'),
                'secret'   => config('open_secret'),
                'token'    => config('open_token'),
                'aes_key'  => config('open_aeskey'),
                'oauth' => [
                    'scopes'   => ['snsapi_base'],
                    'callback' => '/api/login/wechat_callback_main',
                ]
            ];
            $this->openPlatform=Factory::openPlatform($config);
            $mallInfo = Mall::getOneById(1);
            $officialAccount = $this->openPlatform->officialAccount($mallInfo['authorizer_appid'],$mallInfo['authorizer_refresh_token']);
            $oauth = $officialAccount->oauth->scopes(['snsapi_base']);
            return $oauth->redirect()->send();
        } */
        //$user_id=2;
        $token=LoginService::S()->checkStatus($user_id);
        cookie('session_token',$token, 300);
        $url  =  cookie('redirect_url');
		$this->redirect($url);
        //header('Location: '.);exit;
    }

    /* 所有分公司都用mall_id=1的信息做一次授权记录openid */
    public function wechat_callback_main () {
        $user_id = cookie('user_id');
        $mallInfo = Mall::getOneById(1);
        $officialAccount = $this->openPlatform->officialAccount($mallInfo['authorizer_appid'],$mallInfo['authorizer_refresh_token']);
        $user = $officialAccount->oauth->user()->toArray();
        $wechatUser = $user['original'];

        User::where(['id'=>$user_id])->update([
            'openid_main' => $wechatUser['openid']
        ]);

        $token=LoginService::S()->checkStatus($user_id);
        $url  =  cookie('redirect_url')."&session_token={$token}";
        $this->redirect($url);
    }

    /* 检查登录态 */
    public function checkToken () {
        $input = input();
        if(!$input['session_token']) {
            $this->ajax(203,'正在登陆...');
        }
        $uid = LoginService::S()->checkTokenBackUid($input['session_token']);
        if($uid) {
            $userInfo = User::getOne([
                'id' => $uid,
                'mall_id' => $this->mall_id
            ]);
            if(!$userInfo) {
                $uid = false;
            }
        }
        if(!$uid) {
            $this->ajax(203,'正在登陆...');
        }
        $this->ajax(200,'success');
    }
}
